package com.gamecollection.mvc;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 游戏引擎类
 * 负责协调Model、View、Controller组件
 */
public class GameEngine extends JFrame {
    
    private Model model;
    private View view;
    private Controller controller;
    
    private Image offScreenImage;
    private int width;
    private int height;
    private String title;
    
    public GameEngine(Model model, View view, Controller controller, int width, int height, String title) {
        this.model = model;
        this.view = view;
        this.controller = controller;
        this.width = width;
        this.height = height;
        this.title = title;
        
        initialize();
    }
    
    private void initialize() {
        // 设置窗口属性
        setTitle(title);
        setSize(width, height);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        
        // 初始化MVC组件
        model.initialize();
        view.initialize();
        controller.initialize();
        
        // 添加事件监听器
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                controller.handleKeyPressed(e);
            }
            
            @Override
            public void keyReleased(KeyEvent e) {
                controller.handleKeyReleased(e);
            }
        });
        
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                controller.handleMouseClicked(e);
            }
        });
        
        addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                controller.handleMouseMoved(e);
            }
        });
        
        // 启动游戏循环
        startGameLoop();
    }
    
    private void startGameLoop() {
        Timer timer = new Timer(40, e -> {
            // 更新模型
            model.update();
            
            // 更新控制器
            controller.update();
            
            // 更新视图
            view.update();
            
            // 重绘界面
            repaint();
        });
        timer.start();
    }
    
    @Override
    public void paint(Graphics g) {
        // 双缓冲
        if (offScreenImage == null) {
            offScreenImage = createImage(width, height);
        }
        
        Graphics gImage = offScreenImage.getGraphics();
        
        // 清空背景
        gImage.setColor(Color.BLACK);
        gImage.fillRect(0, 0, width, height);
        
        // 渲染视图
        view.render(gImage);
        
        // 将缓冲图像绘制到屏幕
        g.drawImage(offScreenImage, 0, 0, null);
    }
    
    public void launch() {
        setVisible(true);
        requestFocus();
    }
    
    // Getter方法
    public Model getModel() {
        return model;
    }
    
    public View getView() {
        return view;
    }
    
    public Controller getController() {
        return controller;
    }
} 